<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>

<style type="text/css">
/* table sorter */
table.tablesorter {
	width: 100%;
	text-align: left;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
	background-color: #e6EEEE;
}
table.tablesorter thead tr .tblsort-header {
	cursor: pointer;
	background: url(/images/tablesorter/bg.gif) no-repeat right center;
}
table.tablesorter tbody td {
	color: #3D3D3D;
	background-color: #FFF;
	vertical-align: top;
}
table.tablesorter tbody tr.odd td {
	background-color:#F0F0F6;
}
table.tablesorter thead tr .headerSortUp {
	background: url(/images/tablesorter/asc.gif) no-repeat right center;
}
table.tablesorter thead tr .headerSortDown {
	background: url(/images/tablesorter/desc.gif) no-repeat right center;
}
table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
background-color: #8dbdd8;
}
/* end table sorter */
</style>
<div class="middle padtop10 myclear">
	<div class="page-content">
		<h3>Report</h3>
		<hr class="martop10 marbottom10 myclear" />
		
		<br />
		<table cellpadding="0" cellspacing="0" border="0" class="">
			<tr>
				<td class="ver-middle padbottom10 padright10">
					<b>Theo ngày</b>
				</td>
				<td class="ver-middle padbottom10">
					<input type="text" value="<?php echo $fromdate . ' :: ' . $todate; ?>" id="date" name="date" class="input-time" style="width: 200px;"/>
				</td>
				
				<td class="ver-middle padbottom10 padleft10">
					<input type="button" value="      View      " onclick="viewDomainReport();" />
				</td>
			</tr>
		</table>
		<br />
		
		<table cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td class="ver-middle padbottom10 padright10">
					<b>Tìm kiếm</b>
				</td>
				<td class="ver-middle padbottom10 padright10">
					<input type="text" id="txtClientDomain" style="padding: 4px 5px; width: 200px;" />
				</td>
				<td class="ver-middle padbottom10 padright10">
					<select id="slcSearchType">
						<option value="1">Tìm kiếm theo tên</option>
						<option value="2">Theo Giá</option>
					</select>
				</td>
				
				<td class="ver-middle padbottom10 padright10">
					<input type="text" id="txtClientDomain2" style="padding: 4px 5px; width: 200px;" />
				</td>
				<td class="ver-middle padbottom10 padright10">
					<select id="slcSearchType2">
						<option value="1">Tìm kiếm theo tên</option>
						<option value="2">Theo Giá</option>
					</select>
				</td>
				<td class="ver-middle padbottom10 padright10" colspan="2">
					<input type="button" value="Search" onclick="searchDomain();" />
				</td>
			</tr>
		</table>
		
		<p class="padtop10">
			<span class="ver-middle">Quick Filter: </span> 
			<select class="ver-middle" id="slcQuickFilter" style="width: 200px;" onchange="cpcdomain_quickfilter(this.value);">
				<option value="0">Tất cả</option>
				<option value="1">Nội bộ</option>
				<option value="2">Đối tác</option>
			</select>
			<input type="hidden" id="hddPartnerCrc" value="<?php echo $strPartnerCrc; ?>" />
		</p>
		
		<br />
		
		<table class="martop10" cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td>
					<b>Tổng số zones đang xem: </b> <b id="numViewZone" style="color: #ff6600; font-size: 14px;"><?php echo $numDomain; ?></b>
					<span class="cursor-pointer marleft10" style="display: none;" id="viewAll" onclick="showAll();">(Xem tất cả)</span>
				</td>
			</tr>
		</table>
		
		<table class="martop5 marbottom5" cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td>
					<input type="button" value="Xem dữ liệu theo ngày" onclick="domain_bydate_chart(1);" />
				</td>
				<td class="padleft20">
					<input type="button" value="Xem dữ liệu theo ngày với các domain đang chọn" onclick="domain_bydate_chart(2);" />
				</td>
				<td class="padleft20">
					<input type="button" value="Export to .CVS" onclick="cpcdomain_export_2csv();" />
				</td>
			</tr>
		</table>
		
		<div style="width: 980px; overflow: auto;">
			<table id="tableData" class="martop10 tbl-list tablesorter" cellpadding="0" cellspacing="0" border="0">
				<thead>
					<tr>
						<th class="td-label text-center tblsort-header" width="60" style="background-image: none;">
							<input type="checkbox" id="chkAllDomain" onclick="chkAllDomain(this, '.chkDomainItem');"/>
						</th>
						
						<th class="td-label tblsort-header"  style="background-image: none;">
							<b>Domain</b>
						</th>
						
						<th class="td-label tblsort-header" width="90" style="background-image: none;">
							<b>Group Domain</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="60" style="background-image: none;">
							<b>Price</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Total Click</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Real Click</b>
						</th>
												
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Total View</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="70" style="background-image: none;">
							<b>CTR (%)</b>
						</th>	
					</tr>
				</thead>
				<tbody id="tbodyListDomain">
					<?php
					$index = 1;
					foreach($lstDomain as $domain)
					{
						$odd = ($index%2 == 1) ? "" : "odd";
						?>
						<tr  class="tr_domain <?php echo $odd; ?>">
							<td class="pad5 text-center" >
								<input type="checkbox" class="chkDomainItem" onclick="chkDomainItem();" />
							</td>
														
							<td class="pad5" >
								<?php echo $domain['name']; ?>
							</td>
							
							<td class="pad5" >
								<span>
									<?php echo $domain['domaingroup']; ?>
								</span>
								<input type="hidden" class="hddgcrc" value="<?php echo $domain['groupcrc'] ?>" />
							</td>
							
							<td class="pad5 text-center" >
								<?php echo $domain['price'];; ?>
							</td>
														
							<td class="pad5 text-right" >
								<?php echo ($domain['totalclick'] == '' || $domain['totalclick'] == -1) ? 'N/A' : number_format($domain['totalclick']); ?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($domain['realclick'] == '' || $domain['realclick'] == -1) ? 'N/A' : number_format($domain['realclick']); ?>
							</td>
														
							<td class="pad5 text-right" >
								<?php echo ($domain['totalview'] == '' || $domain['totalview'] == -1) ? 'N/A' : number_format($domain['totalview']); ?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($domain['ctr'] == -1 || $domain['ctr'] == '') ? 'N/A' : $domain['ctr']; ?>
							</td>
							
						</tr>
						<?php
						$index++;
					}
					?>
				</tbody>
				<tfoot>
					<tr id="trTotalAll">
						<td colspan="4" class="pad5 bold">
							Tổng
						</td>
						<td class="pad5 text-right bold">
							<span id="totalClick"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalRealClick"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalView"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalCtr"></span>
						</td>
					</tr>
					<tr id="trTotalAllFilter" style="display: none;">
						<td colspan="4" class="pad5 bold">
							Tổng
						</td>
						<td class="pad5 text-right bold">
							<span id="totalClickFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalRealClickFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalViewFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalCtrFilter"></span>
						</td>
					</tr>
				</tfoot>
			</table>
		</div>
		
		<br />
		<div id="divByDate"></div>
		
		<br /><br />
		
	</div>
</div>
<form id="frmExpCvs" action="<?php echo site_url('report/cpcdomain/?exp=cvs&fdate=' . $fromdate . '&tdate=' . $todate) ?>" method="post" style="display: none;">
	<textarea id="txtexp" name="txtexp" ></textarea>
</form>

<form id="frmDateExpCvs" action="<?php echo site_url('report/cpcdomain/?exp=cvsdate&fdate=' . $fromdate . '&tdate=' . $todate) ?>" method="post" style="display: none;">
	<textarea id="txtexpdate" name="txtexpdate" ></textarea>
</form>
<img alt="" src="/images/ajax-loader.gif" id="imgLoadingPopup" style="display: none;" />
<div class="ajaxLoadingPopupBackground" id="ajaxLoadingPopupBackground"></div>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/highcharts.src.js"></script>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/modules/exporting.src.js"></script>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/themes/grid.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$('input#date').DatePicker({
			format: 'd-m-Y',
			date: ['<?php echo $fromdate;?>','<?php echo $todate;?>'],
        	current: '<?php echo date('d-m-Y');?>',
			position: 'right',
			calendars: 2,
        	mode: 'range',
			onChange: function(formated, dates) 
			{
				$('#date').val(formated.join(' :: '));
				$('#date').DatePickerHide();				
			}
		});
		
		$.tablesorter.addWidget({ 
		    // give the widget a id 
		    id: "repeatHeaders", 
		    // format is called when the on init and when a sorting has finished 
		    format: function(table) { 
		        // cache and collect all TH headers 
		        if(!this.headers) { 
		            var h = this.headers = [];  
		            $("thead th",table).each(function() { 
		                h.push( 
		                    "" + $(this).text() + "" 
		                );
		            }); 
		        } 
		         
		        // remove appended headers by classname. 
		        $("tr.repated-header",table).remove(); 
		         
		        // loop all tr elements and insert a copy of the "headers"     
		        for(var i=0; i < table.tBodies[0].rows.length; i++) { 
		            // insert a copy of the table head every 10th row 
		            if((i%5) == 4) { 
		                $("tbody tr:eq(" + i + ")",table).before( 
		                    $("").html(this.headers.join("")) 
		                 
		                );     
		            } 
		        } 
		    } 
		}); 
	
		jQuery.tablesorter.addParser({
	      id: "fancyNumber",
	      is: function(s) {
	        return false;
	      },
	      format: function(s) {
	        return parseFloat(s.replace(/,/g,'')) ;
	      },
	      type: "numeric"
	    });
	    	    
	    <?php
	    if($cPage < $pCount)
	    {
    	?>
    		loadAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
			domain_ajax_view_type('<?php echo $fromdate; ?>', '<?php echo $todate; ?>', '<?php echo $cPage + 1; ?>', '<?php echo $totalClick; ?>', '<?php echo $totalRealClick; ?>', '<?php echo $totalView ?>');
    	<?php
	    }
		?>
	});
	
	function domain_ajax_view_type(fromdate, todate, cPage, totalclick, totalrealclick, totalview)
	{
		var _onSuccess = function(data)
		{
			var objData = jQuery.parseJSON(data);
			if(objData.login == 'login' || objData.right == 'notright')
			{
				window.location.reload(true);
				return;
			}
			else
			{
				if($.trim(objData.data) != '')
				{
					$('#tbodyListDomain').append(objData.data);
					var currPage = parseInt(objData.cPage);
					var totalPage = parseInt(objData.pCount);
					if(currPage < totalPage)
					{
						currPage = currPage + 1;
						domain_ajax_view_type(fromdate, todate, currPage, objData.totalclick, objData.totalrealclick, objData.totalview);
					}
					else
					{
						$('#totalClick').html(objData.totalclick);
						$('#totalRealClick').html(objData.totalrealclick);
						$('#totalView').html(objData.totalview);
						$('#totalCtr').html(objData.totalctr);
						var myTextExtraction = function(node)
					    {  
					    	var cellIndex = node.cellIndex;
					    	var rel = '';
					    	switch(cellIndex)
					    	{
					    		case 0:
					    			break;
								case 1:
					                rel = $.trim($(node).html());
					    			break;
				 				case 2:
					                var arrSpan = $(node).find('span');
					                if(arrSpan.length > 0)
					                {
					                   rel = $.trim($(arrSpan[0]).html());
					                }
					                break;
					    		case 3:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
					    		case 4:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
					    		case 5:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
				    			case 6:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
				    			case 7:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
					    	}
					    	return rel;
					    };
				    	
						$('#tableData').tablesorter(
					    {
					    	textExtraction: myTextExtraction,
							widgets: ['zebra','repeatHeaders'],
					        headers: 
					        {
						        0: {sorter: false},
						        1: {sorter: 'text'},
						        2: {sorter: 'text'},
						        3: {sorter: 'numeric'},
						        4: {sorter: 'numeric'},
						        5: {sorter: 'numeric'},
						        6: {sorter: 'numeric'},
						        7: {sorter: 'numeric'}
					        }
					    });
					    setTimeout('domain_bydate_chart(1)', 1000);
					}
				}
			}
		};
		var _onError = function()
		{
			disableAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
		};
		getAjax('/admin/report/cpcdomain_ajax', 'fd=' + fromdate + '&td=' + todate + '&p=' + cPage + '&ttc=' + totalclick + '&ttrc=' + totalrealclick + '&ttv=' + totalview, '', '', '', false, _onSuccess, _onError);
	}
	
	function viewDomainReport()
	{
		var arrTime = $('#date').val().split('::');
 		var fromdate = $.trim(arrTime[0]);
        var todate = $.trim(arrTime[1]);
		window.location = '/admin/report/cpcdomain?fdate=' + fromdate + '&tdate=' + todate;
	}
	
	function searchDomain()
	{
		var key = $.trim($('#txtClientDomain').val());
		key = key.toLowerCase();
		var type = $.trim($('#slcSearchType').val());
		
		var keyFilter = $.trim($('#txtClientDomain2').val());
		keyFilter = keyFilter.toLowerCase();
		var chkFilter = (keyFilter != '') ? true : false;
		
		var typeFilter = $.trim($('#slcSearchType2').val());
		if(key == '')
		{
			alert('Bạn hãy nhập từ khóa!');
			$('#txtClientDomain').focus();
			return;
		}
		var arrTr = $('#tableData tbody#tbodyListDomain tr.tr_domain');
		var arrHide = new Array();
		var arrShow = new Array();
		var numShow = 0;
		var numTr = arrTr.length;
		
		for(var i=0; i < numTr; i++)
		{
			var tmp = '';
			var tmp2 = '';
			var tmpFilter = '';
			var tmpFilter2 = '';
			
			var arrTd = $(arrTr[i]).find('td');
			if(type== '1')
			{
				tmp = $.trim($(arrTd[2]).html());
				tmp2 = $.trim($(arrTd[1]).html());
			}
			else if(type == '2')
			{
				tmp = $.trim($(arrTd[3]).html());	
			}
			
			tmp = tmp.toLowerCase();
			tmp2 = tmp2.toLowerCase();
			var index = tmp.indexOf(key, 0);
			var index2 = tmp2.indexOf(key, 0);
			var indexFilter = -1;
			var indexFilter2 = -1;
			if(chkFilter != '')
			{
				if(typeFilter== '1')
				{
					tmpFilter = $.trim($(arrTd[2]).html());
					tmpFilter22 = $.trim($(arrTd[1]).html());
				}
				else if(typeFilter == '2')
				{
					tmpFilter = $.trim($(arrTd[3]).html());	
				}
				
				tmpFilter = tmpFilter.toLowerCase();
				tmpFilter2 = tmpFilter2.toLowerCase();
				indexFilter = tmpFilter.indexOf(keyFilter, 0);
				indexFilter2 = indexFilter.indexOf(keyFilter, 0);
			}
			
			if(tmp != '')
			{
				if(index > -1 || index2 > -1)
				{
					if(chkFilter)
					{
						if(indexFilter > -1 || indexFilter2 > -1)
						{
							numShow++;
							arrShow.push(arrTr[i]);
						}
						else
						{
							arrHide.push(arrTr[i]);
						}
					}
					else
					{
						numShow++;
						arrShow.push(arrTr[i]);
					}
				}
				else
				{
					arrHide.push(arrTr[i]);
				}
			}
			else
			{
				arrHide.push(arrTr[i]);
			}
		}
		
		if(numShow > 0)
		{
			$('#numViewZone').html(numShow);
			$('#viewAll').show();
			
			jQuery.each(arrHide, function(){$(this).hide();});
			var tmpindex = 0;
			var totalClick = 0;
			var totalRealClick = 0;		
			var totalView = 0;
			jQuery.each(arrShow, function(){
				$(this).removeClass('odd');
				var arrTd = $(this).find('td');
				var click = $.trim($(arrTd[4]).html());
				click = click.replace(/,/g,'');
   				click = (click == 'N/A' || click == '' ) ? 0 : parseFloat(click);
   				totalClick += click;
   				
   				var rclick = $.trim($(arrTd[5]).html());
				rclick = rclick.replace(/,/g,'');
   				rclick = (rclick == 'N/A' || rclick == '' ) ? 0 : parseFloat(rclick);
   				totalRealClick += rclick;
   				   				
   				var view = $.trim($(arrTd[6]).html());
				view = view.replace(/,/g,'');
   				view = (view == 'N/A' || view == '' ) ? 0 : parseFloat(view);
   				totalView += view;
   				
				var cl = tmpindex%2 == 0 ? 'odd' : '';
				$(this).addClass(cl);
				$(this).show();
				tmpindex++;
			});
			
			if(totalView > 0)
			{
				var ctr = roundNumber((totalRealClick/totalView)*100,3);
				$('#totalCtrFilter').html(ctr);
			}
			else
			{
				$('#totalCtrFilter').html('N/A');
			}
			
			$('#totalClickFilter').html(addCommas(totalClick));
			$('#totalRealClickFilter').html(addCommas(totalRealClick));
			$('#totalViewFilter').html(addCommas(totalView));
			$('#trTotalAll').hide();
			$('#trTotalAllFilter').show();
		}
		else
		{
			alert('Không tìm thấy kết quả nào.');
			return;
		}
	}
	
	function showAll()
	{
		var arr = $('#tableData tbody#tbodyListDomain tr.tr_domain');
		$('#numViewZone').html(arr.length);
		$('#viewAll').hide();
		var tmpindex = 0;
		jQuery.each(arr, function(){
			$(this).removeClass('odd');
			var arrTd = $(this).find('td');
			var cl = tmpindex%2 == 0 ? 'odd' : '';
			$(this).addClass(cl);
			$(this).show();
			tmpindex++;
		});
		$('#trTotalAll').show();
		$('#trTotalAllFilter').hide();
		$('#txtClientDomain').val('');
		$('#slcSearchType').val('1');
		$('#txtClientDomain2').val('');
		$('#slcSearchType2').val('1');
	}
	
	function chkAllDomain(obj, cssClass)
	{
		if($(obj).attr('checked'))
		{
			var arrTr = $('#tableData tbody#tbodyListDomain tr.tr_domain');
			var arr = $('#tableData tbody ' + cssClass);
			var numTr = arrTr.length;
			var strid = '';
			for(var i=0; i < numTr; i++)
			{
				var tmp = '';
				if($(arrTr[i]).css('display') != 'none')
				{
					var id = $.trim($(arr[i]).val());
					$(arr[i]).attr('checked', true);
					strid += (strid == '') ? id : ',' + id;
				}
			}
		}
		else
		{
			$('#chkAllDomain').val('');
			$('#tableData ' + cssClass).attr('checked', false);
		}
	}
	
	function chkDomainItem()
	{
		var arr = $('#tableData tbody#tbodyListDomain .chkDomainItem');
		var isAll = true;
		var num = arr.length;
		for(var i = 0; i < num; i++)
		{
			if(!$(arr[i]).attr('checked'))
			{
				isAll = false;
				break;
			}
		}
		$('#chkAllDomain').attr('checked', isAll);
	}
	
	function cpcdomain_export_2csv()
	{
		var arrTr = $('#tableData tbody#tbodyListDomain tr.tr_domain');
		var arrShow = new Array();
		var num = arrTr.length;
		
		for(var i = 0; i < num; i++)
		{
			if($.trim($(arrTr[i]).css('display')) != 'none')
			{
				arrShow.push(arrTr[i]);
			}
		}
		var listDomain = new Object();
		var num = arrShow.length;
		var index = 0;
		if(num > 0)
		{
			for(var i = 0; i < num; i++)
			{
				var tmp = new Object();
				var arrTd = $(arrShow[i]).find('td');
				tmp['domain'] = $.trim($(arrTd[1]).html());
				tmp['gdomain'] = $.trim($(arrTd[2]).find('span').html());
				tmp['price'] = $.trim($(arrTd[3]).html());
				tmp['tc'] = $.trim($(arrTd[4]).html());
				tmp['rc'] = $.trim($(arrTd[5]).html());
				tmp['v'] = $.trim($(arrTd[6]).html());
				tmp['ctr'] = $.trim($(arrTd[7]).html());
				
				arrTd = null;
				listDomain[index] = tmp;
				tmp = null;
				index++;
			}
			var arrTrTotal = $('#tableData tfoot tr');
			if(arrTrTotal.length > 0)
			{
				for(var i=0; i < arrTrTotal.length; i++)
				{
					if($(arrTrTotal[i]).css('display') != 'none')
					{
						var tmp = new Object();
						var arrTd = $(arrTrTotal[i]).find('td');
						tmp['domain'] = $.trim($(arrTd[0]).html());
						tmp['gdomain'] = '';
						tmp['price'] = '';
						tmp['tc'] = $.trim($(arrTd[1]).find('span').html());
						tmp['rc'] = $.trim($(arrTd[2]).find('span').html());
						tmp['v'] = $.trim($(arrTd[3]).find('span').html());
						tmp['ctr'] = $.trim($(arrTd[4]).find('span').html());
						listDomain[index] = tmp;
						tmp = null;
					}
				}
			}
			$('#txtexp').val(JSON.stringify(listDomain));
			listDomain = null;
			$('#frmExpCvs').submit();
		}
		else
		{
			alert('Bạn chưa chọn domain nào để export.');
			return;
		}
	}
	
	function cpcdomain_date_export_2csv()
	{
		var arrTr = $('#tblByDate tbody tr');
		var num = arrTr.length;
		
		var listDate = new Object();
		var index = 0;
		if(num > 0)
		{
			for(var i = 0; i < num; i++)
			{
				var tmp = new Object();
				var arrTd = $(arrTr[i]).find('td');
				tmp['dt'] = $.trim($(arrTd[0]).html());
				tmp['tc'] = $.trim($(arrTd[1]).html());
				tmp['rc'] = $.trim($(arrTd[2]).html());
				tmp['v'] = $.trim($(arrTd[3]).html());
				tmp['ctr'] = $.trim($(arrTd[4]).html());
				
				arrTd = null;
				listDate[index] = tmp;
				tmp = null;
				index++;
			}
			var arrTrTotal = $('#tblByDate tfoot tr');
			if(arrTrTotal.length > 0)
			{
				var tmp = new Object();
				var arrTd = $(arrTrTotal[0]).find('td');
				tmp['dt'] = $.trim($(arrTd[0]).html());
				tmp['tc'] = $.trim($(arrTd[1]).html());
				tmp['rc'] = $.trim($(arrTd[2]).html());
				tmp['v'] = $.trim($(arrTd[3]).html());
				tmp['ctr'] = $.trim($(arrTd[4]).html());
				listDate[index] = tmp;
				tmp = null;
			}
			$('#txtexpdate').val(JSON.stringify(listDate));
			listDate = null;
			$('#frmDateExpCvs').submit();
		}
		else
		{
			alert('Không có dữ liệu để export');
			return;
		}
	}
	
	function domain_bydate_chart(type)
	{
		var arrTime = $('#date').val().split('::');
 		var fromdate = $.trim(arrTime[0]);
        var todate = $.trim(arrTime[1]);
		var arr = $('#tableData tbody#tbodyListDomain tr.tr_domain');
		strcrc = '';
		var numSelect = 0;
		
		jQuery.each(arr, function(){
			if($.trim($(this).css('display')) != 'none')
			{
				if(type == 2)
				{
					var arrTd = $(this).find('td');
					var arrCheckBox = $(arrTd[0]).find('input');
					if($(arrCheckBox[0]).attr('checked'))
					{
						var arrInput = $(arrTd[2]).find('input');
						var domaincrc = $.trim($(arrInput[0]).val());
						if(domaincrc != '')
						{
							strcrc += (strcrc == '') ? domaincrc : ',' + domaincrc;
						}
						numSelect++;
					}
				}
				else if(type == 1)
				{
					var arrTd = $(this).find('td');
					var arrInput = $(arrTd[2]).find('input');
					var domaincrc = $.trim($(arrInput[0]).val());
					if(domaincrc != '')
					{
						strcrc += (strcrc == '') ? domaincrc : ',' + domaincrc;
					}
					numSelect++;
				}
				
			}
		});
		if(type == 2)
		{
			if(strcrc == '')
			{
				alert('Bạn chưa chọn domain nào.');
				return;
			}
		}
		strcrc = (numSelect == arr.length) ? '' : strcrc;
		var _onSuccess = function(data)
		{
			if($.trim(data) == 'notright' || $.trim(data) == 'login')
			{
				window.location.reload(true);
				return;
			}
			else
			{
				$('#divByDate').html(data);
				disableAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
			}
		};
		var _onError = function()
		{
			disableAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
		};
		loadAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
		getAjax('/admin/report/cpcdomain_ajax_bydate', 'fd=' + fromdate + '&td=' + todate + '&crc=' + strcrc, '', 'POST', '', false, _onSuccess, _onError);
	}
	
	function cpcdomain_quickfilter(val)
	{
		if(val == 0)
		{
			showAll();
		}
		else if(val == 1 || val == 2)
		{
			var strParnterCrc = $.trim($('#hddPartnerCrc').val());
			var arrTr = $('#tableData tbody#tbodyListDomain tr.tr_domain');
			var arrHide = new Array();
			var arrShow = new Array();
			var numShow = 0;
			var numTr = arrTr.length;
			
			for(var i=0; i < numTr; i++)
			{
				var arrTd = $(arrTr[i]).find('td');
				var crc = '-' + $.trim($(arrTd[2]).find('input').val()) + ',';			
				if(val == 1)
				{
					if(strParnterCrc.indexOf(crc) == -1)
					{
						arrShow.push(arrTr[i]);
						numShow++;
					}
					else
					{
						arrHide.push(arrTr[i]);
					}
				}
				else
				{
					if(strParnterCrc.indexOf(crc) != -1)
					{
						arrShow.push(arrTr[i]);
						numShow++;
					}
					else
					{
						arrHide.push(arrTr[i]);
					}
				}
			}
			
			if(numShow > 0)
			{
				$('#numViewZone').html(numShow);
				$('#viewAll').show();
				
				jQuery.each(arrHide, function(){$(this).hide();});
				var tmpindex = 0;
				var totalClick = 0;
				var totalRealClick = 0;		
				var totalView = 0;
				jQuery.each(arrShow, function(){
					$(this).removeClass('odd');
					var arrTd = $(this).find('td');
					var click = $.trim($(arrTd[4]).html());
					click = click.replace(/,/g,'');
	   				click = (click == 'N/A' || click == '' ) ? 0 : parseFloat(click);
	   				totalClick += click;
	   				
	   				var rclick = $.trim($(arrTd[5]).html());
					rclick = rclick.replace(/,/g,'');
	   				rclick = (rclick == 'N/A' || rclick == '' ) ? 0 : parseFloat(rclick);
	   				totalRealClick += rclick;
	   				   				
	   				var view = $.trim($(arrTd[6]).html());
					view = view.replace(/,/g,'');
	   				view = (view == 'N/A' || view == '' ) ? 0 : parseFloat(view);
	   				totalView += view;
	   				
					var cl = tmpindex%2 == 0 ? 'odd' : '';
					$(this).addClass(cl);
					$(this).show();
					tmpindex++;
				});
				
				if(totalView > 0)
				{
					var ctr = roundNumber((totalRealClick/totalView)*100,3);
					$('#totalCtrFilter').html(ctr);
				}
				else
				{
					$('#totalCtrFilter').html('N/A');
				}
				
				$('#totalClickFilter').html(addCommas(totalClick));
				$('#totalRealClickFilter').html(addCommas(totalRealClick));
				$('#totalViewFilter').html(addCommas(totalView));
				$('#trTotalAll').hide();
				$('#trTotalAllFilter').show();
			}
		}
	}
</script>

